package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.ProductionReport;
import com.qidianit.mes.pro.mapper.*;
import com.qidianit.mes.pro.service.ProductionReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductionReportServiceImpl implements ProductionReportService {

    @Autowired
    private ProductionReportMapper productionReportMapper;
    @Autowired
    private ProductionOrderMapper productionOrderMapper;
    @Autowired
    private ProcessMapper processMapper;
    @Autowired
    private WorkstationMapper workstationMapper;
    @Autowired
    private DictDataMapper dictDataMapper;

    /*============================ 分页多条件查询 ============================*/
    @Override
    public Result<PageInfo<ProductionReport>> selectProductionReport(ProductionReport report,
                                                                     int pageNum,
                                                                     int pageSize) {
        // 仅需启用一次分页插件
        PageHelper.startPage(pageNum, pageSize);
        List<ProductionReport> list = productionReportMapper.selectByCondition(report);
        // 封装 PageInfo（自动包含分页信息）
        PageInfo<ProductionReport> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    /*============================ 编辑 ============================*/
    @Override
    public Result updateProductionReport(ProductionReport report) {
        // 存在性校验
        if (productionReportMapper.selectById(report.getId()) == null) {
            return Result.error("报工单不存在");
        }
        return productionReportMapper.updateProductionReport(report) == 1
                ? Result.success()
                : Result.error("更新失败");
    }

    /*============================ 状态变更公共校验 ============================*/
    private Result changeStatus(Long id, Integer targetStatus) {
        ProductionReport po = productionReportMapper.selectById(id);
        if (po == null) {
            return Result.error("报工单不存在");
        }
        // 可扩展状态机校验（例如：不允许从已删除状态变更）
        if (po.getDeleteFlag() == 1) {
            return Result.error("已删除的报工单不允许变更状态");
        }
        return productionReportMapper.updateStatus(id, targetStatus) == 1
                ? Result.success()
                : Result.error("状态更新失败");
    }

    @Override
    public Result updateStatus1(Long id) {
        return changeStatus(id, 31); // 已确认
    }

    @Override
    public Result updateStatus2(Long id) {
        return changeStatus(id, 32); // 通过
    }

    @Override
    public Result updateStatus3(Long id) {
        return changeStatus(id, 33); // 不通过
    }

    /*============================ 单条查询 ============================*/
    @Override
    public Result<ProductionReport> selectById(Long id) {
        if (id == null) {
            return Result.error("ID不能为空");
        }
        ProductionReport report = productionReportMapper.selectById(id);
        if (report == null) {
            return Result.error("未查询到报工单信息");
        }
        return Result.success(report);
    }

    /*============================ 删除 ============================*/
    @Override
    public Result deleteById(Long id) {
        if (id == null) {
            return Result.error("ID不能为空");
        }
        // 存在性校验
        if (productionReportMapper.selectById(id) == null) {
            return Result.error("报工单不存在");
        }
        return productionReportMapper.deleteById(id) == 1
                ? Result.success()
                : Result.error("删除失败");
    }

    @Override
    @Transactional
    public Result batchDelete(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Result.error("请选择需要删除的报工单");
        }
        // 批量删除前校验是否存在无效ID
        for (Long id : ids) {
            if (productionReportMapper.selectById(id) == null) {
                return Result.error("ID为" + id + "的报工单不存在，批量删除失败");
            }
        }
        return productionReportMapper.batchDelete(ids) > 0
                ? Result.success()
                : Result.error("批量删除失败");
    }
}